'''
模块名称：audio_player.py
功能：封装音频播放命令发送功能
'''

from machine import UART
from machine import FPIOA

class AudioPlayer:
    def __init__(self, tx_pin=11, rx_pin=12, baudrate=9600):
        """
        初始化音频播放器

        参数:
            tx_pin: 发送引脚
            rx_pin: 接收引脚
            baudrate: 波特率
        """
        self.fpioa = FPIOA()
        self.fpioa.set_function(tx_pin, FPIOA.UART2_TXD)
        self.fpioa.set_function(rx_pin, FPIOA.UART2_RXD)

        self.uart = UART(UART.UART2, baudrate)

        # 定义16进制命令
        self.CMD_FIGHT =  b'\x7E\xFF\x06\x03\x00\x00\x01\xEF'  # 打架
        self.CMD_FALL =   b'\x7E\xFF\x06\x03\x00\x00\x02\xEF'  # 跌倒
        self.CMD_GATHER = b'\x7E\xFF\x06\x03\x00\x00\x04\xEF'  # 聚集
        self.CMD_RUN =    b'\x7E\xFF\x06\x03\x00\x00\x03\xEF'  # 奔跑

    def send_command(self, command):
        """发送命令到音频模块"""
        self.uart.write(command)
        print(f"发送命令: {command.hex()}")

    def play_fight_sound(self):
        """播放打架音效"""
        self.send_command(self.CMD_FIGHT)

    def play_fall_sound(self):
        """播放跌倒音效"""
        self.send_command(self.CMD_FALL)

    def play_gather_sound(self):
        """播放聚集音效"""
        self.send_command(self.CMD_GATHER)

    def play_run_sound(self):
        """播放奔跑音效"""
        self.send_command(self.CMD_RUN)

    def deinit(self):
        """释放资源"""
        self.uart.deinit()

    def read(self):
        return self.uart.read(32)

